Skip to content

Allow users to choose the bbox clamping mode #9128

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jun 30, 2025

Conversation

NicolasHug
Copy link
Member

@NicolasHug NicolasHug commented Jun 27, 2025

Closes #8254

This PR:

  • Adds a clamping_mode parameter to to the BoundingBoxes() constructor which is stored as a metadata attribute. The default is "hard" for now because this is what happens in main. We will change it to "soft" as soon as Adjust clamping for rotated bboxes #9112 is merged.
  • Adds the clamping_mode parameter to all necessary kernels.
  • Adds the clamping_mode parameter to the ClampBoundingBoxes() method. The default here is None, which defaults to the bbox's clamping_mode attribute. @AntoineSimoulin @scotts that's a different default from what we had originally discussed, but I think it makes sense?
  • Adds the SetClampingMode() transform which just sets the clamping_mode attribute of a bboc and can safely be used within a Compose() pipeline.
  • For now, only "hard" and "none" are implemented. Next step will be to merge Adjust clamping for rotated bboxes #9112 to support "soft" mode and make it the default.

I left a bunch of non-critical TODOBB todos in the code, which I'll address later, but before the release. The type checker is failing, I'll address before merging, please review regardless :)

cc @vfdev-5

Copy link

pytorch-bot bot commented Jun 27, 2025

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/vision/9128

Note: Links to docs will display an error until the docs builds have been completed.

This comment was automatically generated by Dr. CI and updates every 15 minutes.

Comment on lines 69 to 72
def transform(self, inpt: tv_tensors.BoundingBoxes, params: dict[str, Any]) -> tv_tensors.BoundingBoxes:
out = inpt.clone()
out.clamping_mode = self.clamping_mode
return out
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@AntoineSimoulin @scotts in our chat I raised a potential problem with the clamping_mode leaking out of this transform. I think I made our conversation a lot more complicated that it needed to be. We can avoid the "leak" by just calling clone(), as we should, which returns a copy.
No need to make Compose() work like a context manager. I added corresponding tests.

@@ -46,6 +46,14 @@ def is_rotated_bounding_format(format: BoundingBoxFormat) -> bool:
)


# TODOBB consider making this a Literal instead. Tried briefly and got
# torchscript errors, leaving to str for now.
# CLAMPING_MODE_TYPE = Literal["hard", "soft", "none"]
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Regarding the acceptable values, I'm not a fan of "none", it might be confused with None. Maybe "no-clamping" is better? Regardless, if that's OK with you I think we can leave this (nonetheless important!) bikeshedding for later PR.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed that None and "none" are bound (ha!) to get confused and that we should definitely choose something else.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we consider having possible values be either "hard", "soft" and None (no value provided) instead of having the "none" value as a string?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that would be a good option, we'll have to make sure this doesn't conflict with the None default parameter of ClampBoundingBoxes and its kernel. Let's consider this soon in a follow-up

@scotts
Copy link
Contributor

scotts commented Jun 27, 2025

Adds the clamping_mode parameter to the ClampBoundingBoxes() method. The default here is None, which defaults to the bbox's clamping_mode attribute. @AntoineSimoulin @scotts that's a different default from what we had originally discussed, but I think it makes sense?

As long as the box itself must have a value (and I think that's the case), then I think that makes sense.

Copy link
Contributor

@scotts scotts left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's a testament to the transform's design that this was so easy to add! This also makes me even more comfortable we made the right design decision, since we don't need to touch the individual transforms.

@NicolasHug NicolasHug merged commit 9235ee1 into pytorch:main Jun 30, 2025
41 of 45 checks passed
Copy link

Hey @NicolasHug!

You merged this PR, but no labels were added.
The list of valid labels is available at https://github.com/pytorch/vision/blob/main/.github/process_commit.py

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Allow bounding boxes transforms to *not* clamp
4 participants